import Vue from 'vue'
import VueRouter from 'vue-router'
import shopLogin from '../components/shopLogin.vue'
import shopHome from '../components/shopHome.vue'
import shopWelcome from '../components/shopWelcome.vue'
import homeUser from '../components/user/homeUser.vue'
import right from '../components/power/userRights.vue'
import roles from '../components/power/userRoles.vue'
import cate from '../components/goods/goodCate.vue'
import list from '../components/goods/goodList.vue'
import add from '../components/goods/add.vue'
import order from '../components/order/order.vue'
import report from '../components/report/report.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: shopLogin
  },
  {
    path: '/home',
    component: shopHome,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: shopWelcome
    },
    {
      path: '/users',
      component: homeUser
    },
    {
      path: '/rights',
      component: right
    },
    {
      path: '/roles',
      component: roles
    },
    {
      path: '/categories',
      component: cate
    },
    {
      path: '/goods',
      component: list
    },
    {
      path: '/goods/add',
      component: add
    },
    {
      path: '/orders',
      component: order
    },
    {
      path: '/reports',
      component: report
    }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //  to是将要访问的路径
  //  from 是从哪个路径来的
  //  next 是一个放行函数next(),next('') 强制跳转
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
